package xworker;

import org.xmeta.Category;
import org.xmeta.Thing;
import org.xmeta.ThingManager;
import org.xmeta.World;

import java.io.File;
import java.util.Iterator;

public class ExtractModelToResources {
    public static void main(String[] args){
        try{
            World world = World.getInstance();
            world.init("./xworker/");
            world.addFileThingManager("resources", new File("./src/main/resources/"), false, false);

            ThingManager resourceManager = world.getThingManager("resources");
            Thread.sleep(10000);
            for(ThingManager thingManager : world.getThingManagers()){
                if(thingManager.getName().equals("resources")){
                    continue;
                }

                System.out.println(thingManager.getName());
                Iterator<Thing> iter = thingManager.iterator(null, true);
                while(iter.hasNext()){
                    Thing thing = iter.next();
                    System.out.println(thing.getMetadata().getPath());
                    String categoryName = thing.getMetadata().getCategory().getName();
                    Category category = resourceManager.getCategory(categoryName);
                    if(category == null){
                        resourceManager.createCategory(categoryName);
                        category = resourceManager.getCategory(categoryName);
                    }
                    thing.getMetadata().setCategory(category);
                    resourceManager.save(thing);
                }
            }

        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
